package Controler.Eleve;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import JDBC.Eleve_Afficher;
import JDBC.Eleve_Supprimer;
import Model.Listes;
import Model.Eleve;
import Model.Famille;
import Model.Lieu;

/**
 * Servlet implementation class Eleve_ChoixAction
 */
@WebServlet("/Eleve_ChoixAction")
public class Eleve_ChoixAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Eleve_ChoixAction() {
        super();
        // TODO Auto-generated constructor stub
    }

	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		HttpSession session = request.getSession();
		Listes listes = (Listes) session.getAttribute("listes");
		
		if (listes==null)
		{
			listes = new Listes();
		}
		
		Eleve_Afficher listeEleve = new Eleve_Afficher();
		ArrayList<Eleve> mesEleves = listeEleve.afficher();
		listes.setMesEleves(mesEleves);
		session.setAttribute("listes", listes);
		request.getRequestDispatcher("Vues/Eleve/vueGRP1_tableauEleve.jsp").forward(request, response);
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		String url = "Vues/Eleves/vueGRP1_tableauEleve.jsp";
		
		HttpSession session = request.getSession();
		Listes listes = (Listes) session.getAttribute("listes");


		
		if (listes==null)
		{
			listes = new Listes();
		}


		Eleve_Afficher listeEleve = new Eleve_Afficher();
		ArrayList<Eleve> mesEleves = listeEleve.afficher();
		listes.setMesEleves(mesEleves);
		
		
		if(request.getParameter("ajout")!=null)
		{
			url="Vues/Eleve/vueGRP1_ajouter.jsp";
		}
		
		
		if(request.getParameter("modification")!=null)
		{
			for (int i=0;i<mesEleves.size();i++){
				Eleve elevemodif;
				if (mesEleves.get(i).getIdEleve()==Integer.parseInt(request.getParameter("modification"))){
					elevemodif=mesEleves.get(i);
					request.setAttribute("el", elevemodif);
				}
			}
			session.setAttribute("", listes);
			url="Vues/Eleve/vueGRP1_modifier.jsp";
		}
		
		
		if(request.getParameter("affichage")!=null)
		{
			for (int i=0;i<mesEleves.size();i++){
				Eleve elevemodif;
				if (mesEleves.get(i).getIdEleve()==Integer.parseInt(request.getParameter("affichage"))){
					elevemodif=mesEleves.get(i);
					System.out.println(elevemodif.toString());
					request.setAttribute("eleve", elevemodif);
				}
			}
			session.setAttribute("", listes);
			url="Vues/Eleve/vueGRP1_afficher.jsp";
		}
		
		
		if(request.getParameter("suppression")!=null)
		{
			int ide = Integer.parseInt(request.getParameter("suppression"));

			Eleve_Supprimer suppression = new Eleve_Supprimer();
			suppression.supprimer(ide);
			
			listeEleve = new Eleve_Afficher();
			mesEleves = listeEleve.afficher();
			listes.setMesEleves(mesEleves);
			session.setAttribute("listes", listes);
			
			url="Vues/Eleve/vueGRP1_tableauEl.jsp";
		}
		
		request.getRequestDispatcher(url).forward(request, response);
	}
}

